/**
 * Created by DoveXL on 2018/12/6.
 */
$(document).ready(function () {
    document.getElementById('VolunteerPartInfo').style.display='none';
    queryUser(null,null,null,null,null,0);
});

function UserChangeByUserType() {
    var Value = $('input[name="UserCategory"]:checked').val();
    if(Value=="AllVolunteer")
    {
        document.getElementById('VolunteerPartInfo').style.display='none';
        $("#volunteerunit").val("");
        $("#volunteername").val("");
        $("#IDCardNumber").val("");
        $("#AllGender").attr("checked","checked");
        queryUser(null,null,null,null,null,0);
    }else if (Value=="PartVolunteer")
    {
        document.getElementById('VolunteerPartInfo').style.display='block';
    }
}


function queryUserByCondition() {
    var volunteerunit = $("#volunteerunit").val().trim();
    var volunteername = $("#volunteername").val().trim();
    var IDCardNumber = $("#IDCardNumber").val().trim();
    var Value = $('input[name="VolunteerCategory"]:checked').val();
    if (volunteerunit.length==0)
    {
        volunteerunit=null;
    }
    if(volunteername.length==0)
    {
        volunteername=null
    }
    if(IDCardNumber.length==0)
    {
        IDCardNumber=null;
    }
    if(Value=="AllGender")
    {
        Value=null;
    }else if(Value=="male")
    {
        Value = "男";
    }else if(Value =="female")
    {
        Value ="女";
    }else
    {
        Value = null;
    }
    queryUser(null,volunteername,Value,volunteerunit,null,0);
}



function queryUser(IDCard,Name,Gender,Unit,Phonenum,Deleted) {
    $.ajax({
        async: true,
        type: "post",
        url: "/VolunteerPlatform/PersonInfo/CertainVolunteerInfo",
        dataType: "json",
        contentType : 'application/json;charset=utf-8',
        data: JSON.stringify({"page":1,"user_flag":Deleted,"pagesize":5,"IDCard":IDCard,"Name":Name,"Gender":Gender,"Unit":Unit,"Phonenum":Phonenum}),
        cache: false,
        success: function(data) {
            var result = eval(data);
            $("#PersonManagementInfo tr").empty();
            for (var i = 0; i<result.pagelist.length; i++) {
                var row = CreatePersonRow(result.pagelist[i]);
                $("#PersonManagementInfo").append(row);
            };
            var currentPage = result.currentpage; //当前页数
            var pageCount = result.pagecount; //总页数
            var options = {
                bootstrapMajorVersion: 3, //版本
                currentPage: currentPage, //当前页数
                totalPages: pageCount, //总页数
                numberOfPages: 5,
                shouldShowPage:true,//是否显示该按钮
                itemTexts: function (type, page, current) {
                    switch (type) {
                        case "first":
                            return "首页";
                        case "prev":
                            return "上一页";
                        case "next":
                            return "下一页";
                        case "last":
                            return "末页";
                        case "page":
                            return page;
                    }
                },
                onPageClicked: function (event, originalEvent, type, page) {
                    $.ajax({
                        async: true,
                        url: "/VolunteerPlatform/PersonInfo/CertainVolunteerInfo",
                        type: "post",
                        dataType : "json",
                        contentType : 'application/json;charset=utf-8',
                        data: JSON.stringify({"page":page,"user_flag":Deleted,"pagesize":5,"IDCard":IDCard,"Name":Name,"Gender":Gender,"Unit":Unit,"Phonenum":Phonenum}),
                        cache: false,
                        success: function (data) {
                            $("#PersonManagementInfo tr").empty();
                            var result = eval(data);
                            for (var i = 0; i <result.pagelist.length; i++) {
                                var row = CreatePersonRow(result.pagelist[i]);
                                $("#PersonManagementInfo").append(row);
                            };
                        }
                    });
                }
            };
            $('#UserpageLimit').bootstrapPaginator(options);
        }
    });
}


function CreatePersonRow(UserInfo) {
    var row = document.createElement('tr');
    var ChooseCell = document.createElement('td');
    var btnLabel = document.createElement('label');
    var btnCheckbox = document.createElement('input');
    btnCheckbox.setAttribute("type","checkbox");
    btnCheckbox.setAttribute("name","PersonCheckbox");
    btnLabel.appendChild(btnCheckbox);
    ChooseCell.appendChild(btnLabel);
    row.appendChild(ChooseCell);
    var UserNameCell = document.createElement('td');
    UserNameCell.innerHTML = UserInfo.user_name;
    row.appendChild(UserNameCell);
    var UserGenderCell = document.createElement('td');
    UserGenderCell.innerHTML =  UserInfo.user_gender;
    row.appendChild(UserGenderCell);
    var UserIDCardCell = document.createElement('td');
    UserIDCardCell.innerHTML = UserInfo.user_IDCard;
    row.appendChild(UserIDCardCell);
    var UserUnitCell = document.createElement('td');
    UserUnitCell.innerHTML = UserInfo.user_Unit;
    row.appendChild(UserUnitCell);
    var UserPhonenumCell = document.createElement('td');
    UserPhonenumCell.innerHTML = UserInfo.user_phonenum;
    row.appendChild(UserPhonenumCell);
    return row;
}


function queryTeamInfo(CategorySelect) {
    $.ajax({
        async: false,
        url:"/VolunteerPlatform/TeamInfo/CertainTeamInfo",
        type:"post",
        dataType:"json",
        contentType : 'application/json;charset=utf-8',
        data: JSON.stringify({"page":2,"pagesize":-1,"TeamName":null}),
        cache: false,
        success:function (CategorySet) {
            var CategorySet = eval(CategorySet);
            $(CategorySelect).empty();
            $("<option></option>").val("PleaseChoose")
                .text("请选择")
                .appendTo(CategorySelect);
            for(var i =0;i<CategorySet.pagelist.length;i++)
            {

                $("<option></option>").val(CategorySet.pagelist[i].user_teamname)
                    .text(CategorySet.pagelist[i].user_teamname)
                    .appendTo(CategorySelect);
            }
        }
    });


}


function ShowAddPersonInfoModal() {
    var CategorySelect = $("#AddPersonInfoUnit");
    queryTeamInfo(CategorySelect);
    $("#AddPersonInfoName").val("");
    $("#AddPersonInfoIDCardNumber").val("");
    $("#AddPersonInfoUnit").val("PleaseChoose");
    $("#AddPersonPhonenum").val("");
    $("#AddPersonInfoFlag").html("");
    $("#BtnAddPersonInfo").removeAttr("disabled");
    $("#BtnAddPersonInfo").removeClass("disabled");
    $("#AddPersonInfoModal").modal();
}




function  AddPersonInfoFunction() {
    var PersonName =  $("#AddPersonInfoName").val().trim();
    var IDCardNumber = $("#AddPersonInfoIDCardNumber").val().trim();
    var Unit = $("#AddPersonInfoUnit").val().trim();
    var Phonenum = $("#AddPersonPhonenum").val().trim();
    if(PersonName.length!=0 && IDCardNumber.length!=0 && Unit.length!=0 && Phonenum.length!=0)
    {
        if(checkChinese("用户姓名",PersonName))
        {
             if(IdentityCodeValid(IDCardNumber))
             {
                 if(Unit!="PleaseChoose") {
                     if (checkPhone(Phonenum)) {
                         $.ajax({
                             async: false,
                             type: "post",
                             url: "/VolunteerPlatform/PersonInfo/checkIDCardNumber",
                             contentType: 'application/json;charset=utf-8',
                             data: JSON.stringify({"IDCardNumber": IDCardNumber}),
                             success: function (data) {
                                 if (data == "1") {
                                     $.ajax({
                                         async: false,
                                         type: "post",
                                         url: "/VolunteerPlatform/PersonInfo/AddVolunteerInfo",
                                         contentType: 'application/json;charset=utf-8',
                                         data: JSON.stringify({"IDCardNumber": IDCardNumber,"PersonName":PersonName,"Unit":Unit,"Phonenum":Phonenum}),
                                         success: function (data) {
                                             if (data == "1") {

                                                 $("#AddPersonInfoFlag").html("添加成功!");
                                                 $("#BtnAddPersonInfo").attr("disabled","disabled");
                                                 $("#BtnAddPersonInfo").addClass("disabled");
                                                 var Value = $('input[name="UserCategory"]:checked').val();
                                                 if(Value=="AllVolunteer")
                                                 {
                                                     queryUser(null,null,null,null,null,0);
                                                 }else if (Value=="PartVolunteer")
                                                 {
                                                     queryUserByCondition();
                                                 }
                                                 return true;

                                             } else {
                                                 $("#AddPersonInfoFlag").html("未能添加成功!");
                                                 return false;
                                             }
                                         }
                                     });
                                 } else {
                                     $("#AddPersonInfoFlag").html("该身份证号已存在，请检查重新填写");
                                     return false;
                                 }
                             }
                         });

                     } else {
                         $("#AddPersonInfoFlag").html("电话号码不正确，请检查重新填写");
                         return false;
                     }
                 }else
                 {
                     $("#AddPersonInfoFlag").html("未选择单位，请检查重新填写");
                     return false;
                 }
             }else
             {
                 $("#AddPersonInfoFlag").html("身份证号格式不正确，请检查重新填写");
                 return false;
             }
        }else
        {
            $("#AddPersonInfoFlag").html("用户名非中文，请检查重新填写");
            return false;
        }
    }else
    {
        $("#AddPersonInfoFlag").html("存在未填项，请检查重新填写");
        return false;
    }

}





function ShowChangePersonInfoModal() {
    var CategorySelect = $("#ChangePersonInfoUnit");
    queryTeamInfo(CategorySelect);
    var count = $("input[name = 'PersonCheckbox']:checked").length;
    if(count==0)
    {
        alert("未选中任何行");
    }else if(count>1)
    {
        alert("选中超过一行!");
    }else
    {
        var CheckedNode = $("input[name = 'PersonCheckbox']:checked").parent().parent();
        var OriginPersonIDCardNumber = $(CheckedNode).next().next().next().html().trim();
        var PersonName = $(CheckedNode).next().html().trim();
        var PersonUnit = $(CheckedNode).next().next().next().next().html().trim();
        var PersonPhonenum = $(CheckedNode).next().next().next().next().next().html().trim();
        $("#OriginPersonInfoIDCardNumber").html(OriginPersonIDCardNumber);
        $("#ChangePersonInfoIDCardNumber").val(OriginPersonIDCardNumber);
        $("#ChangePersonInfoName").val(PersonName);
        $("#ChangePersonInfoUnit").val(PersonUnit);
        $("#ChangePersonPhonenum").val(PersonPhonenum);
        $("#ChangePersonInfoFlag").html("");
        $("#BtnChangePersonInfo").removeAttr("disabled");
        $("#BtnChangePersonInfo").removeClass("disabled");
        $("#ChangePersonInfoModal").modal();
    }
}
function ChangeTeamInfoFunction() {
    var CheckedNode = $("input[name = 'PersonCheckbox']:checked").parent().parent();
    var Name = $(CheckedNode).next().html().trim();
    var IDCardNumber = $(CheckedNode).next().next().next().html().trim();
    var PersonUnit = $(CheckedNode).next().next().next().next().html().trim();
    var PersonPhonenum= $(CheckedNode).next().next().next().next().next().html().trim();

    var NewPersonName = $("#ChangePersonInfoName").html().trim();
    var NewIDCardNumber = $("#ChangePersonInfoIDCardNumber").val().trim();
    var NewUnit = $("#ChangePersonInfoUnit").val().trim();
    var NewPhonenum=$("#ChangePersonPhonenum").val().trim();
    if(NewPersonName.length!=0 && NewIDCardNumber.length!=0&&NewUnit.length!=0&&NewPhonenum.length!=0)
    {
        if(NewPersonName==Name&&NewIDCardNumber==IDCardNumber&&PersonUnit==NewUnit&&NewPhonenum==PersonPhonenum)
        {
            $("#ChangePersonInfoFlag").html("未修改任何信息，不能提交!");
            return false;
        }else
        {
            if(checkChinese("姓名",NewPersonName))
            {
                if(IdentityCodeValid(NewIDCardNumber))
                {
                    if(NewUnit!="PleaseChoose") {
                        if(checkPhone(NewPhonenum))
                        {
                            $.ajax({
                                async: false,
                                type: "post",
                                url: "/VolunteerPlatform/PersonInfo/checkIDCardNumber",
                                contentType: 'application/json;charset=utf-8',
                                data: JSON.stringify({"IDCardNumber": NewIDCardNumber}),
                                success: function (data) {
                                    if (data == "1") {
                                        $.ajax({
                                            async: false,
                                            type: "post",
                                            url: "/VolunteerPlatform/PersonInfo/ChangeVolunteerInfo",
                                            contentType: 'application/json;charset=utf-8',
                                            data: JSON.stringify({"OriginIDCardNumber":IDCardNumber,"IDCardNumber": NewIDCardNumber,"OriginPersonName":Name,"PersonName":NewPersonName,"Unit":NewUnit,"Phonenum":NewPhonenum}),
                                            success: function (data) {
                                                if (data == "1") {
                                                    $("#ChangePersonInfoFlag").html("添加成功!");
                                                    $("#BtnChangeVolunteerInfo").attr("disabled","disabled");
                                                    $("#BtnChangeVolunteerInfo").addClass("disabled");
                                                    var Value = $('input[name="UserCategory"]:checked').val();
                                                    if(Value=="AllVolunteer")
                                                    {
                                                        queryUser(null,null,null,null,null,0);
                                                    }else if (Value=="PartVolunteer")
                                                    {
                                                        queryUserByCondition();
                                                    }
                                                    return true;
                                                } else {
                                                    $("#ChangePersonInfoFlag").html("未能修改成功!");
                                                    return false;
                                                }
                                            }
                                        });
                                    } else {
                                        $("#ChangePersonInfoFlag").html("该身份证号已存在，请检查重新填写");
                                        return false;
                                    }
                                }
                            });
                        }else
                        {
                            $("#ChangePersonInfoFlag").html("联系方式格式有误，不能提交!");
                            return false;
                        }


                    }else
                    {
                        $("#ChangePersonInfoFlag").html("单位格式有误，不能提交!");
                        return false;

                    }

                }else
                {
                    $("#ChangePersonInfoFlag").html("身份证号格式有误，不能提交!");
                    return false;
                }
            }else
            {
                $("#ChangePersonInfoFlag").html("姓名格式有误，不能提交!");
                return false;
            }
        }
    }else {
        $("#ChangePersonInfoFlag").html("必填项存在为空现象!");
        return false;
    }
}






































function ShowExportPersonInfoModal() {
    $("#ExportPersonInfoExcel").css("display","none");
    $("#ExportPersonInfoModal").modal();
}



function ExportExcelContent()
{
    var checkvalue = $('input[name="UserCategory"]:checked').val();
    if(checkvalue=="AllVolunteer")
    {
        var checklist=[];
        checklist.push("Number");
        $("input[name = 'ExportField']:checked").each(function () {
                checklist.push($(this).val());
            }
        );
        if(checklist.length==2)
        {
            alert("任何一个字段都未选中!");
            return false;
        }
        $.ajax({
            async: false,
            type: "post",
            url: "/VolunteerPlatform/PersonInfo/ExportPersonInfo",
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify({"checkFieldName": checklist, "IDCardNumber":null, "Name":null, "Unit":null,"Gender":null}),
            success: function (data) {
                if(data=="1")
                {
                    $("#ExportPersonInfoExcel").css("display","block");
                }else
                {
                }
            }
        });
    }else if(checkvalue=="PartVolunteer")
    {
        var volunteerunit = $("#volunteerunit").val().trim();
        var volunteername = $("#volunteername").val().trim();
        var IDCardNumber = $("#IDCardNumber").val().trim();
        var Value = $('input[name="VolunteerCategory"]:checked').val();
        if (volunteerunit.length==0)
        {
            volunteerunit=null;
        }else
        {
            volunteerunit = "%"+volunteerunit+"%";
        }

        if(volunteername.length==0)
        {
            volunteername=null
        }else
        {
            volunteername="%"+volunteername+"%";
        }

        if(IDCardNumber.length==0)
        {
            IDCardNumber=null;
        }else
        {
            IDCardNumber="%"+IDCardNumber+"%";
        }
        if(Value=="AllGender")
        {
            Value=null;
        }else if(Value=="male")
        {
            Value = "%"+"男"+"%";
        }else if(Value =="female")
        {
            Value ="%"+"女"+"%";
        }else
        {
            Value = null;
        }
        var checklist=[];
        checklist.push("Number");
        $("input[name = 'ExportField']:checked").each(function () {
                checklist.push($(this).val());
            }
        );
        if(checklist.length==2)
        {
            alert("任何一个字段都未选中!");
            return false;
        }
        $.ajax({
            async: false,
            type: "post",
            url: "/VolunteerPlatform/PersonInfo/ExportPersonInfo",
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify({"checkFieldName": checklist, "IDCardNumber":IDCardNumber, "Name":volunteername, "Unit":volunteerunit,"Gender":Value}),
            success: function (data) {
                if(data=="1")
                {
                    $("#ExportPersonInfoExcel").css("display","block");
                }else
                {
                }
            }
        });
    }else
    {

    }
}

function DisplayDownload() {

    $("#ExportPersonInfoExcel").css("display","none");
}


function ShowDeletePersonInfoModal() {
    var count = $("input[name = 'PersonCheckbox']:checked").length;
    if(count==0)
    {
        alert("未选中任何行");
    }else if(count>1)
    {
        alert("选中超过一行!");
    }else
    {
        var CheckedNode = $("input[name = 'PersonCheckbox']:checked").parent().parent();
        var Name = $(CheckedNode).next().html().trim();
        var IDCardNumber = $(CheckedNode).next().next().next().html().trim();
        var PersonUnit = $(CheckedNode).next().next().next().next().html().trim();
        var PersonPhonenum= $(CheckedNode).next().next().next().next().next().html().trim();
        $("#DeletePersonInfoIDCardNumber").html(IDCardNumber);
        $("#DeletePersonInfoName").html(Name);
        $("#DeletePersonInfoUnit").html(PersonUnit);
        $("#DeletePersonInfoPhonenum").html(PersonPhonenum);
        $("#DeletePersonInfoFlag").html("");
        $("#BtnDeletePersonInfo").removeAttr("disabled");
        $("#BtnDeletePersonInfo").removeClass("disabled");
        $("#DeletePersonInfoModal").modal();
    }
}



function DeletePersonInfoFunction() {
    var CheckedNode = $("input[name = 'PersonCheckbox']:checked").parent().parent();
    var IDCardNumber = $(CheckedNode).next().next().next().html().trim();
    $.ajax({
        async: false,
        type: "post",
        url: "/VolunteerPlatform/PersonInfo/DeletePersonInfo",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify({
            "IDCardNumber":IDCardNumber
        }),
        cache: false,
        success: function (data) {
            if (data == "1") {
                $("#DeletePersonInfoFlag").html("删除成功");
                var Value = $('input[name="UserCategory"]:checked').val();
                if(Value=="AllVolunteer")
                {
                    queryUser(null,null,null,null,null,0);
                }else if (Value=="PartVolunteer")
                {
                    queryUserByCondition();
                }
                $("#BtnDeletePersonInfo").attr("disabled","disabled");
                $("#BtnDeletePersonInfo").addClass("disabled");
            } else {
                $("#DeletePersonInfoFlag").html("删除失败");
            }
        }
    })

}